﻿using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Layout;
using iText.Layout.Element;
using TableTools.Models;

namespace TableTools.Handlers
{
    public class PdfTableHandler : ITableHandler
    {
        public TableResult ToTable(string path)
        {
            TableResult result = new TableResult();

            using PdfReader reader = new PdfReader(path);
            using PdfDocument pdfDoc = new PdfDocument(reader);
            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
            {
                PdfPage page = pdfDoc.GetPage(i);
                for (int j = 0; j < page.GetPdfObject().GetAsArray(PdfName.Contents).Size(); j++)
                {
                    PdfObject element = page.GetPdfObject().GetAsArray(PdfName.Contents).Get(j);
                    if (element.IsStream())
                    {
                        string text = PdfTextExtractor.GetTextFromPage(page);
                        StringReader sr = new StringReader(text);
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            if (line.Contains("TABLE START"))
                            {
                                var table = new TableTemplate();
                                var rows = new List<string[]>();
                                line = sr.ReadLine();
                                string[] cells = line.Split('\t');
                                if (cells.Any(x => !string.IsNullOrWhiteSpace(x)))
                                {
                                    rows.Add(cells);
                                }
                                while ((line = sr.ReadLine()) != null)
                                {
                                    if (line.Contains("TABLE END"))
                                    {
                                        int maxCol = rows.Max(x => x.Length);
                                        table.Table = new TableInfo(rows.Count - 1, maxCol);
                                        for (int b = 0; b < rows[0].Length; b++)
                                        {
                                            table.Table.Headers[b] = rows[0][b];
                                        }
                                        for (int a = 0; a < rows.Count - 1; a++)
                                        {
                                            var cols = rows[a + 1];
                                            for (int b = 0; b < cols.Length; b++)
                                            {
                                                table.Table.Rows[a][b] = cols[b];
                                            }
                                        }
                                        result.Add(table);
                                        break;
                                    }
                                    cells = line.Split('\t');
                                    if (cells.Any(x => !string.IsNullOrWhiteSpace(x)))
                                    {
                                        rows.Add(cells);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
            pdfDoc.Close();

            return result;
        }
    }
}
